import Axios from "axios"
import { addPending, removePending, clearPending,getMap } from "./map"

const CancelToken = Axios.CancelToken;


Axios.interceptors.request.use(config => {
  const source=CancelToken.source();

    let cancel;
    new CancelToken(c => {
      cancel = c
    })

    console.log("getMap",getMap())
    config.cancelToken=source.token
  
    removePending({
      ...config,
      cancelToken: cancel
    }) // 在请求开始前，对之前的请求做检查取消操作
    addPending({
      ...config,
      cancelToken: cancel,
      source
    }) // 将当前请求添加到 pending 中
    return config
  }, error => {
    return Promise.reject(error)
  })
  
  Axios.interceptors.response.use(response => {
    removePending(response.config) // 在请求结束后，移除本次请求
    return response
  }, error => {
    if (Axios.isCancel(error)) {
      console.log('repeated request: ' + error.message)
    }
    return Promise.reject(error)
  })

export default Axios